package com.demo.server.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.server.entity.Permission;
import com.demo.server.entity.User;
import com.demo.server.entity.req.PermissionIconSetReq;
import com.demo.server.mapper.PermissionMapper;
import com.demo.server.mapper.RolePermissionMapper;
import com.demo.server.util.AccessTokenUtil;
import com.demo.server.util.Result;
import com.demo.server.util.ResultData;

@Service
public class PermissionService {

	public static final String PERMISSON_TYPE_MAINMENU = "01";
	public static final String PERMISSON_TYPE_SUBMENU = "02";
	public static final String PERMISSON_TYPE_BUTTON = "03";
	
	public static final String PERMISSION_EXIST = "资源已存在";
	public static final String CHILDREN_PERMISSION_EXIST = "存在子功能";
	
	public static final Integer MAINMENU_PID = 0;
	
	@Autowired
	PermissionMapper permissionMapper;
	
	@Autowired
	RolePermissionMapper rolePermissionMapper;
	
	/**
	 * @title 列出所有资源 
	 * @return
	 */
	public ResultData<List<Permission>> list(){
		List<Permission> pers = permissionMapper.selectAll();
		return ResultData.success(pers);
	}
	
	/**
	 * @title 列出角色下资源
	 * @param roleId
	 * @return ResultData
	 */
	public ResultData<List<Permission>> listByRoleId(String roleId){
		List<Permission> persmissions = permissionMapper.selectByRoleId(roleId);
		return ResultData.success(persmissions);
	}
	
	/**
	 * @title 列出当前用户资源
	 * @param roleId
	 * @return ResultData
	 */
	public ResultData<List<Permission>> listUserPermission(){
		User user = AccessTokenUtil.getUser();
		List<Permission> persmissions = permissionMapper.selectByUserId(user.getId());
		return ResultData.success(persmissions);
	}
	
	/**
	 * @title 列出当前用户菜单
	 * @param roleId
	 * @return ResultData
	 */
	public ResultData<List<Permission>> listUserMenu(){
		User user = AccessTokenUtil.getUser();
		List<Permission> persmissions = permissionMapper.selectMenuByUserId(user.getId());
		return ResultData.success(persmissions);
	}
	
	/**
	 * @title 添加资源 
	 * @param req
	 * @return
	 */
	public Result add(Permission req) {
		// TODO Auto-generated method stub
		if(PERMISSON_TYPE_MAINMENU.equals(req.getType())) {
			req.setPid(MAINMENU_PID);
		}else {
			Permission existP = permissionMapper.selectByUrl(req.getUrl());
			if(existP != null) {
				return  Result.fail(PERMISSION_EXIST);
			}
		}
		
		if(StringUtils.isNotEmpty(req.getTitle()) && !PERMISSON_TYPE_BUTTON.equals(req.getType()))  {
			Permission exist = permissionMapper.selectByTitle(req.getTitle());
			if(exist != null &&!exist.getId().equals(req.getId()) ) {
				return  Result.fail(PERMISSION_EXIST);
			}
		}
		permissionMapper.insertSelective(req);
		return Result.success();
	}
	
	/**
	 * @title 修改资源
	 * @param req
	 * @return Result
	 */
	public Result edit(Permission req) {
		// TODO Auto-generated method stub
		if(!StringUtils.isEmpty(req.getUrl())) {
			Permission exist = permissionMapper.selectByUrl(req.getUrl());
			if(exist != null &&!exist.getId().equals(req.getId()) ) {
				return  Result.fail(PERMISSION_EXIST);
			}
		}
		if(StringUtils.isNotEmpty(req.getTitle()) && !PERMISSON_TYPE_BUTTON.equals(req.getType()))  {
			Permission exist = permissionMapper.selectByTitle(req.getTitle());
			if(exist != null &&!exist.getId().equals(req.getId()) ) {
				return  Result.fail(PERMISSION_EXIST);
			}
		}
		permissionMapper.updateByPrimaryKeySelective(req);
		return Result.success();
	}
	
	/**
	 * @title 删除资源 
	 * @param id
	 * @return Result
	 */
	public Result delete(Integer id) {
		// TODO Auto-generated method stub
		Permission req = new Permission();
		req.setPid(id);
		List<Permission> children = permissionMapper.select(req);
		if(children != null && children.size() > 0) {
			return Result.fail(CHILDREN_PERMISSION_EXIST);
		}
		permissionMapper.deleteByPrimaryKey(id);
		rolePermissionMapper.deleteByPermissionId(id);
		return Result.success();
	}
	
	/**
	 * @title 重置图标 
	 * @param id
	 * @return Result
	 */
	public Result resetIcon(List<PermissionIconSetReq> req) {
		// TODO Auto-generated method stub
		for(PermissionIconSetReq item : req) {
			permissionMapper.updateIconByUrl(item);
		}
		return Result.success();
	}
}
